/*
 * MainFrame.java
 *
 * Created on Sreda, 7 april 2010, 16:03
 */

package splash.gui;

import splash.core.Settings;
import splash.core.Util;
import splash.core.logging.Logger;
import splash.core.connection.ConsoleUI;

import javax.swing.JOptionPane;

/**
 *
 * @author  Gasper
 */
public class MainFrame extends javax.swing.JFrame implements splash.core.OutControl {
    
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
    }
    
    private Logger log;
    private Settings settings;
    private ConsoleUI session;
    private splash.gui.Users frm;
    private boolean visibility = false;
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        jSeparator1 = new javax.swing.JSeparator();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jProgressBar1 = new javax.swing.JProgressBar();
        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

        getContentPane().setLayout(null);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("~ Splash II ~");
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(677, 530));
        setResizable(false);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(10, 60, 650, 30);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/start.png")));
        jButton1.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                startServer(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt)
            {
                startInfo(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt)
            {
                startInfoOff(evt);
            }
        });

        getContentPane().add(jButton1);
        jButton1.setBounds(70, 160, 110, 110);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/stop.png")));
        jButton2.setEnabled(false);
        jButton2.setMaximumSize(new java.awt.Dimension(74, 25));
        jButton2.setMinimumSize(new java.awt.Dimension(74, 25));
        jButton2.setPreferredSize(new java.awt.Dimension(74, 25));
        jButton2.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                stopServer(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt)
            {
                stopInfo(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt)
            {
                stopInfoOff(evt);
            }
        });

        getContentPane().add(jButton2);
        jButton2.setBounds(500, 160, 110, 110);

        jPasswordField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jPasswordField1.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseEntered(java.awt.event.MouseEvent evt)
            {
                gesloInfo(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt)
            {
                gesloInfoOff(evt);
            }
        });
        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter()
        {
            public void keyTyped(java.awt.event.KeyEvent evt)
            {
                updatePassword(evt);
            }
        });

        getContentPane().add(jPasswordField1);
        jPasswordField1.setBounds(240, 160, 210, 20);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(" ~ glavno geslo ~");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(235, 140, 220, 15);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("~ status ~");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(240, 250, 210, 15);

        jLabel3.setFont(new java.awt.Font("Arial Black", 1, 12));
        jLabel3.setForeground(new java.awt.Color(0, 128, 255));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("\u010dakam na zagon");
        jLabel3.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.lightGray));
        getContentPane().add(jLabel3);
        jLabel3.setBounds(240, 270, 210, 17);

        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(10, 350, 650, 20);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 360, 650, 40);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("~ mo\u010d gesla ~");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(239, 190, 210, 15);

        jProgressBar1.setMaximum(10);
        jProgressBar1.setStringPainted(true);
        jProgressBar1.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseEntered(java.awt.event.MouseEvent evt)
            {
                strengthInfo(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt)
            {
                strengthInfoOff(evt);
            }
        });

        getContentPane().add(jProgressBar1);
        jProgressBar1.setBounds(240, 210, 210, 20);

        jPanel1.setLayout(null);

        jPanel1.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                switchSettings(evt);
            }
        });

        jButton4.setText("Uporabniki");
        jButton4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                editUsers(evt);
            }
        });

        jPanel1.add(jButton4);
        jButton4.setBounds(300, 10, 103, 25);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/run.png")));
        jLabel6.setText("ADC");
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                switchSettings(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt)
            {
                jLabel6MouseEntered(evt);
            }
        });

        jPanel1.add(jLabel6);
        jLabel6.setBounds(560, 0, 45, 40);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/about.png")));
        jLabel7.setText("HLP");
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                showHelp(evt);
            }
        });

        jPanel1.add(jLabel7);
        jLabel7.setBounds(610, 0, 75, 40);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 400, 660, 40);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void showHelp(java.awt.event.MouseEvent evt)//GEN-FIRST:event_showHelp
    {//GEN-HEADEREND:event_showHelp
	new Help().main(null);
    }//GEN-LAST:event_showHelp

    private void jLabel6MouseEntered(java.awt.event.MouseEvent evt)//GEN-FIRST:event_jLabel6MouseEntered
    {//GEN-HEADEREND:event_jLabel6MouseEntered
// TODO add your handling code here:
    }//GEN-LAST:event_jLabel6MouseEntered

    private void switchSettings(java.awt.event.MouseEvent evt)//GEN-FIRST:event_switchSettings
    {//GEN-HEADEREND:event_switchSettings
	//jButton4.setVisible(!jButton4.isVisible());
	
	visibility = !visibility;
	
	jButton4.setVisible(visibility);
	
    }//GEN-LAST:event_switchSettings

    private void editUsers(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editUsers
    {//GEN-HEADEREND:event_editUsers
	this.frm = new Users(this);
	frm.setVisible(true);
    }//GEN-LAST:event_editUsers

    public void closeUserForm()
    {
        if(frm == null)
            return;

        frm.setVisible(false);
        frm = null;
        this.setFocusable(true);
    }

    //@Override
    public void setState(Settings.State s)
    {
        /* Switch? */
        if(s.equals(Settings.State.LOCK))
        {
            this.jLabel3.setText("stre�nik je zaradi varnosti zaklenjen");
        }
        
        if(s.equals(Settings.State.START))
        {
            this.jLabel3.setText("stre�nik je zagnan");
            /* Ponastavi gumbe */
            this.jPasswordField1.setEnabled(false);
            this.jButton2.setEnabled(true);
            this.jButton1.setEnabled(false);
        }
        
        if(s.equals(Settings.State.STOP))
        {
            this.jLabel3.setText("stre�nik je ustavljen");
            this.jButton2.setEnabled(false);
            this.jButton1.setEnabled(true);
            this.jPasswordField1.setEnabled(true);
        }
        
        if(s.equals(Settings.State.STARTUP))
        {
            this.jLabel3.setText("zaganjam stre�nik");
        }
        
        if(s.equals(Settings.State.SHUTDOWN))
        {
            this.jLabel3.setText("zaustavljam stre�nik");
        }
    }

    //@Override
    public Settings.State state()
    {
        return settings.getState();
    }
    
    public void announce(String txt)
    {
        JOptionPane.showMessageDialog(this, txt);
    }
    
    private void startServer(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startServer
        /* Preveri mo� gesla */
        int moc = Util.passwordStrength(new String(this.jPasswordField1.getPassword()));
        if(moc < 5)
        {
            /* Geslo je dokaj�nje �ibko */
            JOptionPane.showMessageDialog(this, "Geslo je pre�ibko");
            this.jLabel3.setText("�akam na zagon");
            return;
        }
        
        /* Popravi napis */

        this.settings.setState(Settings.State.STARTUP);
        /* Varnost, ah varnost :x */
        this.settings.setPassword(new String(this.jPasswordField1.getPassword()));

        this.session = new ConsoleUI(log, settings, this);
        new Thread(this.session).start();

    }//GEN-LAST:event_startServer

    private void strengthInfoOff(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_strengthInfoOff
        this.jLabel4.setText("");
    }//GEN-LAST:event_strengthInfoOff

    private void strengthInfo(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_strengthInfo
        this.jLabel4.setText("Stopnja varnosti izbranega gesla. Priporo�ena je nad polovico.");
    }//GEN-LAST:event_strengthInfo

    private void updatePassword(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_updatePassword
        this.jProgressBar1.setValue(Util.passwordStrength(new String(this.jPasswordField1.getPassword())));
    }//GEN-LAST:event_updatePassword

    private void gesloInfoOff(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gesloInfoOff
        this.jLabel4.setText("");
    }//GEN-LAST:event_gesloInfoOff

    private void gesloInfo(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gesloInfo
        this.jLabel4.setText("Varnostno geslo za uporabo SplashII stre�nika.");
    }//GEN-LAST:event_gesloInfo

    private void stopInfoOff(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stopInfoOff
        this.jLabel4.setText("");
    }//GEN-LAST:event_stopInfoOff

    private void stopInfo(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stopInfo
        this.jLabel4.setText("Ustavi SplashII stre�nik.");
    }//GEN-LAST:event_stopInfo

    private void startInfoOff(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startInfoOff
        this.jLabel4.setText("");
    }//GEN-LAST:event_startInfoOff

    private void startInfo(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startInfo
        this.jLabel4.setText("Za�eni SplashII stre�nik.");
    }//GEN-LAST:event_startInfo

    private void stopServer(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_stopServer
        if(this.session == null)
        {
            return;
        }

        this.session.stopServer();
    }//GEN-LAST:event_stopServer
    
    public void main(Logger log, Settings set) {
        /* Logging and setting setup */
        
        this.log = log;
        this.settings = set;

        /* Sun Microsystems fail */
        /*
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);

            }
        });
         */
        this.setVisible(true);
	this.jButton4.setVisible(false);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables
    
}
